package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjWarningSyBo;
import com.ruoyi.zhgd.domain.vo.ZjWarningSyVo;
import com.ruoyi.zhgd.domain.ZjWarningSy;
import com.ruoyi.zhgd.mapper.ZjWarningSyMapper;
import com.ruoyi.zhgd.service.IZjWarningSyService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 试验通知预警信息Service业务层处理
 *
 * @author cj
 * @date 2023-11-14
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjWarningSyServiceImpl implements IZjWarningSyService {

    private final ZjWarningSyMapper baseMapper;

    /**
     * 查询试验通知预警信息
     */
    @Override
    public ZjWarningSyVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询试验通知预警信息列表
     */
    @Override
    public TableDataInfo<ZjWarningSyVo> queryPageList(ZjWarningSyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjWarningSy> lqw = buildQueryWrapper(bo);
        Page<ZjWarningSyVo> result = baseMapper.selectVoPages(pageQuery.build(), lqw, bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询试验通知预警信息列表
     */
    @Override
    public List<ZjWarningSyVo> queryList(ZjWarningSyBo bo) {
        LambdaQueryWrapper<ZjWarningSy> lqw = buildQueryWrapper(bo);

        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjWarningSy> buildQueryWrapper(ZjWarningSyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjWarningSy> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZjWarningSy::getType, bo.getType());
        lqw.eq(bo.getInsterTime() != null, ZjWarningSy::getInsterTime, bo.getInsterTime());
        lqw.eq(bo.getSyId() != null, ZjWarningSy::getSyId, bo.getSyId());
        lqw.orderByDesc(ZjWarningSy::getInsterTime);
        return lqw;
    }

    /**
     * 新增试验通知预警信息
     */
    @Override
    public Boolean insertByBo(ZjWarningSyBo bo) {
        ZjWarningSy add = BeanUtil.toBean(bo, ZjWarningSy.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改试验通知预警信息
     */
    @Override
    public Boolean updateByBo(ZjWarningSyBo bo) {
        ZjWarningSy update = BeanUtil.toBean(bo, ZjWarningSy.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjWarningSy entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试验通知预警信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    //压力机、万能机 保存预警信息
    @Override
    public void saveWarningSy(String remark, Integer id, String type, Integer message,String deptId) {

        ZjWarningSy s = new ZjWarningSy();
        s.setInsterTime(new Date());
        s.setRemark(remark);
        s.setSyId(id);
        s.setType(type);
        s.setDeptId(deptId);
        //添加 处置状态为未处置
        s.setDisposalStatus(1);
        if (message == 0) {
            s.setMessage("发送成功");
        } else if (message == 1) {
            s.setMessage("发送失败");
        }else if (message == 2) {
            s.setMessage("未选择发送人");
        } else if (message == 3){
            s.setMessage("实验室短信开关未开启");
        }
        baseMapper.insert(s);
    }
}
